package registry

import (
	"github.com/emicklei/go-restful"
	"kubeInstall/handlers"
	"kubeInstall/model/registry"
	"net/http"
	"strconv"
)

func ListProjects(r *restful.Request, resp *restful.Response) {

	opts := new(registry.ListProjectOpts)
	opts.Name = r.QueryParameter("name")
	opts.Owner = r.QueryParameter("owner")
	opts.PageSize, _ = strconv.Atoi(r.QueryParameter("page_size"))
	opts.Page, _ = strconv.Atoi(r.QueryParameter("page"))
	opts.Public, _ = strconv.ParseBool(r.QueryParameter("public"))

	projectDetails, err := registry.ListProject(opts)

	if err != nil {
		handlers.RenderError(resp, "List registry project failed", http.StatusBadRequest)
		return
	}

	resp.WriteHeaderAndJson(http.StatusOK, projectDetails, restful.MIME_JSON)

}
func CreateProject(r *restful.Request, resp *restful.Response) {

	var opts registry.CreateProjectOpts

	if err := r.ReadEntity(&opts); err != nil {
		handlers.RenderError(resp, "get registry project from request failed", http.StatusInternalServerError)
		return
	}

	err := registry.CreateProject(opts)

	if err != nil {
		handlers.RenderError(resp, err.Error(), http.StatusBadRequest)
		return
	}

	resp.WriteHeaderAndJson(http.StatusOK, "", restful.MIME_JSON)

}

func CheckProjectIfExsist(r *restful.Request, resp *restful.Response) {

}
